import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_syt/domain/widgets/tab_indicator.dart';
import 'package:flutter_syt/theme/theme_color.dart';

class ScrollerTabEntity {
  late String type;
  late String label;
  late Widget? page;
  late int showType;
  late String alias;
  late String? id;

  ScrollerTabEntity();

  factory ScrollerTabEntity.create(
    String type,
    String label,
    int showType,
    String alias, {
    Widget? page,
    String? id,
  }) {
    return ScrollerTabEntity()
      ..type = type
      ..label = label
      ..showType = showType
      ..id = id
      ..page = page
      ..alias = alias;
  }

  static List<ScrollerTabEntity> fake() {
    return [
      ScrollerTabEntity.create('news', '新闻', 1, ""),
      ScrollerTabEntity.create('special', '观世运', 2, ""),
      ScrollerTabEntity.create('special', '游成都', 3, ""),
      ScrollerTabEntity.create('special', '活动中心', 2, ""),
      ScrollerTabEntity.create('special', '全民运动', 3, ""),
    ];
  }
}

class NormalTabs extends StatefulWidget {
  final List<ScrollerTabEntity> tabs;
  final TabController tabController;
  final Color? textColor;
  final Color? bgColor;
  // final Alignment alignment;
  final TabAlignment? tabAlignment;

  NormalTabs(
    this.tabs,
    this.tabController, {
    this.textColor = const Color(0xff666666),
    this.bgColor = Colors.white,
    this.tabAlignment = TabAlignment.start,
  });

  @override
  State<NormalTabs> createState() => _NormalTabsState();
}

class _NormalTabsState extends State<NormalTabs>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // alignment: widget.alignment,
      // color: Colors.red,
      // height: 30,
      margin: const EdgeInsets.only(top: 10),
      child: TabBar(
        onTap: (idx) {
          SystemChannels.textInput.invokeMethod('TextInput.hide');
        },
        // padding: EdgeInsets.only(left: 0, top: 12, bottom: 0),
        controller: widget.tabController,
        tabs: _titleWidget(),
        // indicatorSize: TabBarIndicatorSize.label,
        indicatorWeight: 7,
        tabAlignment: widget.tabAlignment,
        // indicatorPadding: EdgeInsets.only(top: 30),
        indicatorSize: TabBarIndicatorSize.tab,
        isScrollable: true,

        labelColor: widget.textColor,
        unselectedLabelStyle:
            TextStyle(fontSize: 16, color: Colors.black.withOpacity(0.6)),
        labelStyle: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        indicator: const LinearGradientTabIndicatorDecoration(
          borderSide: BorderSide(width: 3, color: ThemeColor.primaryColor),
          insets: EdgeInsets.only(left: 20, right: 20, bottom: 0),
          gradient: LinearGradient(
            colors: [
              ThemeColor.colorBtnL1,
              ThemeColor.colorBtnL2,
              ThemeColor.colorBtnL3
            ],
          ),
          isRound: true,
        ),
      ),
    );
  }

  _titleWidget() {
    return widget.tabs.map((e) => Text(e.label)).toList();
  }
}
